#!/bin/bash

echo "🔨 重新构建带AI功能的 cbp2make_ai"

# 清理并重新构建
rm -rf bin .objs
mkdir -p bin .objs

# 编译设置
INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-std=c++11 -DAICODER_ENHANCED -m64"

echo "编译中..."
compile_all() {
    # AI 文件
    g++ $INCLUDES $CXXFLAGS -c src/aicoder_enhancements.cpp -o .objs/aicoder_enhancements.o
    
    # cbp2make 核心文件
    for file in ../cbp2make/src/*.cpp; do
        base=$(basename "$file" .cpp)
        g++ $INCLUDES $CXXFLAGS -c "$file" -o ".objs/$base.o" 2>/dev/null
    done
    
    # STL 工具库
    for file in ../cbp2make/lib/stl*.cpp; do
        base=$(basename "$file" .cpp)
        g++ $INCLUDES $CXXFLAGS -c "$file" -o ".objs/$base.o" 2>/dev/null
    done
    
    # TinyXML 核心库
    g++ $INCLUDES $CXXFLAGS -c ../cbp2make/lib/tinyxml/tinystr.cpp -o .objs/tinystr.o
    g++ $INCLUDES $CXXFLAGS -c ../cbp2make/lib/tinyxml/tinyxml.cpp -o .objs/tinyxml.o
    g++ $INCLUDES $CXXFLAGS -c ../cbp2make/lib/tinyxml/tinyxmlerror.cpp -o .objs/tinyxmlerror.o
    g++ $INCLUDES $CXXFLAGS -c ../cbp2make/lib/tinyxml/tinyxmlparser.cpp -o .objs/tinyxmlparser.o
}

compile_all

# 链接
echo "链接..."
g++ -m64 .objs/*.o -o bin/cbp2make_ai

if [ -f "bin/cbp2make_ai" ]; then
    echo ""
    echo "🎉 带AI功能的重建成功!"
    echo ""
    echo "🧪 测试AI功能:"
    echo "1. 显示AI帮助:"
    ./bin/cbp2make_ai --ai-help
    echo ""
    echo "2. 测试项目分析:"
    ./bin/cbp2make_ai --ai-analyze test.cbp 2>&1 | head -10
    echo ""
    echo "3. 测试交互模式 (输入 quit 退出):"
    echo "quit" | ./bin/cbp2make_ai --ai-interactive 2>&1 | head -5
else
    echo "❌ 构建失败"
fi
